/**
 * losenote.com Inc.
 * Copyright (c) 2013-2013 All Rights Reserved.
 */
package com.losenote.web.control;

import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.time.DateUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import com.losenote.base.LnConstrant;
import com.losenote.base.util.DateUtil;
import com.losenote.base.util.LogUtil;
import com.losenote.model.OperateContext;
import com.losenote.service.FindNoteService;
import com.losenote.service.result.LnResult;
import com.losenote.web.ct.FindNoteFormCt;
import com.losenote.web.form.FindNoteForm;

/**
 * 
 * @author danjingyu
 * @version $Id: FindNotePublishController.java, v 0.1 2013-9-11 上午12:14:19 danjingyu Exp $
 */
@Controller
public class FindNoteController extends BaseController {
    private Logger          logger = Logger.getLogger(IndexController.class);
    @Autowired(required = false)
    private Validator       validator;
    @Autowired
    private FindNoteService findNoteService;

    @RequestMapping("/findNotePublish.htm")
    public ModelAndView initPage(WebRequest request, ModelMap modelMap) {
        ModelAndView view = new ModelAndView("home/findNotePublish");
        consBase(modelMap);
        modelMap.put("categoryId", request.getParameter("categoryId"));
        return view;
    }

    @RequestMapping("/publishFindNote.htm")
    public ModelAndView editFindNote(HttpServletRequest request, FindNoteForm findNoteForm,
                                     BindingResult validError, ModelMap modelMap) {
        ModelAndView currentPage = new ModelAndView("home/findNotePublish");
        LogUtil.info(logger, findNoteForm);
        validator.validate(findNoteForm, validError);
        if (validError.hasErrors()) {
            modelMap.put("findNoteForm", findNoteForm);
            consBase(modelMap);
            modelMap.put("validError", validError);
            return currentPage;
        }
        OperateContext context = getOperateContext(request);
        LnResult result = findNoteService.publishNote(
            FindNoteFormCt.fromForm(findNoteForm, context), context);
        if (!result.isSuccess()) {
            consBase(modelMap);
            modelMap.put("result", result);
        }
        return new ModelAndView(new RedirectView("noteDetail.htm?noteId="
                                                 + result.getRelatedAttr().get(LnConstrant.ID)));
    }

    private ModelMap consBase(ModelMap modelMap) {
        String currentDate = DateUtil.format(new Date(), DateUtil.webFormat);
        String nextWeek = DateUtil.format(DateUtil.addDays(new Date(), 7), DateUtil.webFormat);
        String nextMonth = DateUtil.format(DateUtils.addMonths(new Date(), 1), DateUtil.webFormat);
        String nextHalfYear = DateUtil.format(DateUtils.addMonths(new Date(), 6),
            DateUtil.webFormat);
        String forever = "2100-01-01";
        modelMap.put("currentDate", currentDate);
        modelMap.put("nextWeek", nextWeek);
        modelMap.put("nextMonth", nextMonth);
        modelMap.put("nextHalfYear", nextHalfYear);
        modelMap.put("forever", forever);
        return modelMap;
    }

    public void setFindNoteService(FindNoteService findNoteService) {
        this.findNoteService = findNoteService;
    }

}
